<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class My_account extends MIS_Controller {

	function __construct()
	{
		parent::__construct();
	}
	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/buy_books
	 *	- or -  
	 * 		http://example.com/buy-books
	 *	- or -  
	 * 		http://example.com/buy_books/index
	 *	- or -  
	 * 		http://example.com/buy-books/index
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{	
		$this->loadViewData['banner_inc'] = false;
		$this->loadViewData['upper_footer_inc'] = false;
		$this->loadViewData['search_inc'] = false;
		$this->loadViewData['viewFile'] = 'v_my_account';
		
		$this->load->view('layout', $this->loadViewData);
	}
	public function my_profile()
	{	
		if ($this->uri->segment(3)=="edit")
		{
			// Profile Information Edit Starts
			if($this->uri->segment(4)=="profile") 
			{
				if(count($this->input->post())>0)
				{
					$this->load->library('form_validation');
					$this->form_validation->set_error_delimiters('<div class="form_error">', '</div>');
					$this->form_validation->set_rules('edit_user_profile_name', 'Name', 'trim|required|min_length[6]|max_length[50]');
					$this->form_validation->set_rules('edit_user_profile_email', 'Email', 'trim|required|valid_email');
					$this->form_validation->set_rules('edit_user_profile_phone', 'Phone', 'trim|required|numeric|min_length[10]|max_length[11]');
					$this->form_validation->set_rules('edit_user_profile_address', 'Address', 'trim');
					$this->form_validation->set_rules('edit_user_profile_campus', 'Campus', 'trim');
					$this->form_validation->set_rules('edit_user_profile_university', 'University', 'trim');
					if($this->form_validation->run())
					{
						$data = array(
							'fullname' 		=> $this->input->post('edit_user_profile_name') ,
							'email' 		=> $this->input->post('edit_user_profile_email'),
							'phone' 		=> $this->input->post('edit_user_profile_phone'),
							'present_address' 		=> $this->input->post('edit_user_profile_address'),
							'campus' 		=> $this->input->post('edit_user_profile_campus'),
							'university' 	=> $this->input->post('edit_user_profile_university'),
							'updated'		=> date( 'Y-m-d H:i:s')
						);
						$uid=$this->session->userdata('id_user');
						$this->db->where('id_user', $uid);
						$this->db->update('users', $data); 
						$this->session->set_flashdata('success_mesg', 'Your profile has been saved.');
						redirect('my-account/my-profile');
					}
				}
				
				$query=$this->db->get_where("users", array("id_user"=>$this->session->userdata('id_user')));
				$res=$query->result();
				$this->loadViewData['banner_inc'] = false;
				$this->loadViewData['upper_footer_inc'] = false;
				$this->loadViewData['search_inc'] = false;
				$this->loadViewData['viewFile'] = 'v_my_profile_edit_profile';
				$this->loadViewData['profile_information'] = $res;
			
			$this->load->view('layout', $this->loadViewData);
			}
			// Profile Information Edit Ends
			elseif ($this->uri->segment(4)=="billing") // Billing information Edit Starts
			{
				if(count($this->input->post())>0)
				{
					$this->load->library('form_validation');
					$this->form_validation->set_error_delimiters('<div class="form_error">', '</div>');
					$this->form_validation->set_rules('edit_user_billing_name', 'Name', 'trim|required|min_length[6]|max_length[50]');
					$this->form_validation->set_rules('edit_user_billing_email', 'Email', 'trim|required|valid_email');
					$this->form_validation->set_rules('edit_user_billing_phone', 'Phone', 'trim|required|numeric|min_length[10]|max_length[11]');
					$this->form_validation->set_rules('edit_user_billing_address', 'Address', 'trim');
					$this->form_validation->set_rules('edit_user_billing_campus', 'Campus', 'trim');
					$this->form_validation->set_rules('edit_user_billing_university', 'University', 'trim');
					if($this->form_validation->run())
					{
						$data = array(
							'fullname' 			=> $this->input->post('edit_user_billing_name') ,
							'email' 			=> $this->input->post('edit_user_billing_email'),
							'phone' 			=> $this->input->post('edit_user_billing_phone'),
							'billing_address'	=> $this->input->post('edit_user_billing_address'),
							'campus' 			=> $this->input->post('edit_user_billing_campus'),
							'university' 		=> $this->input->post('edit_user_billing_university'),
							'updated'			=> date( 'Y-m-d H:i:s')
						);
						$uid=$this->session->userdata('id_user');
						$this->db->where('id_user', $uid);
						$this->db->update('users', $data); 
						$this->session->set_flashdata('success_mesg', 'Your profile has been saved.');
						redirect('my-account/my-profile');
					}
				}
				
				$query=$this->db->get_where("users", array("id_user"=>$this->session->userdata('id_user')));
				$res=$query->result();
				$this->loadViewData['banner_inc'] = false;
				$this->loadViewData['upper_footer_inc'] = false;
				$this->loadViewData['search_inc'] = false;
				$this->loadViewData['viewFile'] = 'v_my_profile_edit_billing';
				$this->loadViewData['billing_information'] = $res;
			
				$this->load->view('layout', $this->loadViewData);
			}
			else show_error("Opps! you are in wrong place.", 401, "Invalid access");
		}
		else 
		{
			$query=$this->db->get_where("users", array("id_user"=>$this->session->userdata('id_user')));
			$res=$query->result();
			$this->loadViewData['banner_inc'] = false;
			$this->loadViewData['upper_footer_inc'] = false;
			$this->loadViewData['search_inc'] = false;
			$this->loadViewData['viewFile'] = 'v_my_profile';
			$this->loadViewData['profile_information'] = $res;
			
			$this->load->view('layout', $this->loadViewData);
		}
	}
	public function sell_books()
	{
		$query=$this->db->get_where("book_bank", array("uid"=>$this->session->userdata('id_user')));
		$res=$query->result();
		$query2=$this->db->get_where("users", array("id_user"=>$this->session->userdata('id_user')));
		$res2=$query2->result();
		$this->loadViewData['banner_inc'] = false;
		$this->loadViewData['upper_footer_inc'] = false;
		$this->loadViewData['search_inc'] = false;
		$this->loadViewData['viewFile'] = 'v_my_sell_books';
		$this->loadViewData['sellBookHistory'] = $res;
		$this->loadViewData['user_info'] = $res2;
		$this->load->view('layout', $this->loadViewData);
	}
	public function settings()
	{
		$this->load->library('form_validation');
		$this->form_validation->set_error_delimiters('<div class="form_error">', '</div>');
		$this->form_validation->set_rules('oldPass', 'Old Password', 'trim|required|min_length[6]|max_length[20]|callback__match_password');
		$this->form_validation->set_rules('newPass', 'New Password', 'trim|required|min_length[6]|max_length[20]');
		$this->form_validation->set_rules('confirmPass', 'Confirm Password', 'trim|required|min_length[6]|max_length[20]|matches[newPass]');
		if($this->form_validation->run())
		{
			$data = array(
					'password' 		=> md5($this->input->post('newPass'))
				);
			$uid=$this->session->userdata('id_user');
			$this->db->where('id_user', $uid);
			$this->db->update('users', $data); 
			$this->session->set_flashdata('success_mesg', 'Your password has been changed.');
			redirect('my-account/my-profile');
		}
		$query=$this->db->get_where("users", array("id_user"=>$this->session->userdata('id_user')));
		$res=$query->result();
		$this->loadViewData['banner_inc'] = false;
		$this->loadViewData['upper_footer_inc'] = false;
		$this->loadViewData['search_inc'] = false;
		$this->loadViewData['viewFile'] = 'v_my_profile_settings';
		$this->loadViewData['user_info'] = $res;
		$this->load->view('layout', $this->loadViewData);
	}
	function _match_password()
	{
		$query=$this->db->get_where("users", array("id_user"=>$this->session->userdata('id_user')));
		$res=$query->result();
		foreach ($res as $result)
		{
			if(md5($_POST['oldPass'])==$result->password)
			{
				return TRUE;
			}
			else 
			{
				$this->form_validation->set_message('_match_password','Password mis-matched.');
				return FALSE;
			}
		}
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */